/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = ora_comm.h                                         */
/*  NOTE      =                                                    */
/*  DATE      = 2014/10/09 by songbb                               */
/*******************************************************************/

#ifndef __ORA_UTIL_H_
#define __ORA_UTIL_H_

#include "../ora_core.h"
#include "ora_comm.h"

#if APM_ORA_DBG
    #define PRINT_HEX_STRING(a,b,c) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KCYAN),\
                            printf(a":["),__print_hex_string(b,c),printf("]\n"),\
                            printf(RESET))

    #define ORA_PRINT_PACKET_SPECIFIED(tag, packet, offset, len) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KORANGE),\
                            printf(tag":["),__ora_print_packet_specified(packet,offset,len),printf("]\n"),\
                            printf(RESET))

    #define ORA_PRINT_PACKET_CURRENT(a,b) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KCYAN),\
                            printf(a":["),__ora_print_packet_current(b),printf("]\n"),\
                            printf(RESET))

    #define ORA_PRINT_PACKET(a,b) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KCYAN),\
                            printf(a":["),__ora_print_packet(b),printf("]\n"),\
                            printf(RESET))

    #define ORA_PRINT_TIMEVAL(a,b) (printf("[%s, Line:%u, %s()]",\
                            __FILE__,__LINE__,__func__),\
                            printf(KGREEN),\
                            printf(" %s", a), \
                             __ora_util_print_timeval(b),\
                             printf(RESET))

    #define ORA_PRINT_SESS(...) (printf("[%s, Line:%u, %s()]",\
                            __FILE__,__LINE__,__func__),\
                             __ora_util_print_sess(__VA_ARGS__))

    #define ORA_PRINT_DTY_INFO(...) (printf("[%s, Line:%u, %s()]",\
                                __FILE__,__LINE__,__func__),\
                                printf(KBLUE" "),\
                                 __ora_util_print_dty_info(__VA_ARGS__),\
                                 printf(RESET))
#else
    #define PRINT_HEX_STRING(a,b,c)
    #define ORA_PRINT_PACKET_SPECIFIED(tag, packet, offset, len)
    #define ORA_PRINT_PACKET_CURRENT(a,b)
    #define ORA_PRINT_PACKET(a,b)
    #define ORA_PRINT_TIMEVAL(a,b)
    #define ORA_PRINT_SESS(...)
    #define ORA_PRINT_DTY_INFO(...)
#endif

#define ORA_CHECK_BIT(arr_char, len, pos, value) (arr_char != NULL && len > pos && (arr_char[pos] & value) == value)

#define ORA_PRINT_PACKET_CURRENT_ALWAYS(a,b) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KCYAN),\
                            printf(a":["),__ora_print_packet_current(b),printf("]\n"),\
                            printf(RESET))

#define ORA_PRINT_PACKET_SPECIFIED_ALWAYS(tag, packet, offset, len) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KORANGE),\
                            printf(tag":["),__ora_print_packet_specified(packet,offset,len),printf("]\n"),\
                            printf(RESET))

#define ORA_PRINT_PACKET_ALWAYS(a,b) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KCYAN),\
                            printf(a":["),__ora_print_packet(b),printf("]\n"),\
                            printf(RESET))

#define ORA_PRINT_SESS_ALWAYS(...) (printf("[%s, Line:%u, %s()]",\
                            __FILE__,__LINE__,__func__),\
                             __ora_util_print_sess(__VA_ARGS__))

#define ORA_LOG(fmt, args...) (ora_util_log(__LINE__, __func__, fmt, ##args))

int ora_get_jdbc_type(int i_internal_type);

void ora_bin_to_hex_string(char *p_inbuff, int i_inbuff_len, char *p_outbuff, int i_outbuff_len);

void __print_hex_string(char *p_buff, int i_len);

void __ora_print_packet_specified(L7_ora_packet *p_st_ora_packet, int i_pos, int i_len);

void __ora_print_packet_current(L7_ora_packet *p_st_ora_packet);

void __ora_print_packet(L7_ora_packet *p_st_ora_packet);

char* hex_string_to_char_array(const char *p_src, int i_len);

int ora_util_frtime2timestr(struct timeval *p_tv
                            , char *p_time
                            , unsigned int ui_time_size);

void __ora_util_print_timeval(struct timeval st_timestamp);

void __ora_util_print_sess(L7session_ora *p_st_sess);

void __ora_util_print_dty_info(char *p_str, int i_dty);

void ora_util_log(unsigned int ui_line, const char *p_func, const char *p_fmt, ...);

int ora_util_check_dty(int i_dty);

int ora_util_check_charset(int i_charset);

void ora_util_extract_code_string(char *p_error_mesg
                                , char **pp_code_string);

int ora_check_error_mesg(const char *p_error_mesg);

#endif
